package com.carkeeper.controller;

import com.carkeeper.dto.ServiceDTO;
import com.carkeeper.pojo.ServicePO;
import com.carkeeper.service.ServiceService;
import com.carkeeper.vo.AddServiceVO;
import com.carkeeper.vo.Pager;
import com.carkeeper.vo.ServiceVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: dongxing
 * @date ：Created in 2020/5/25
 * @description ：服务controller
 * @version: 1.0
 */
@RestController
@RequestMapping("/service")
@CacheConfig(cacheNames = "service")
public class ServiceController {
    @Autowired
    private ServiceService serviceService;

    /**
     * 根据id查询服务详情
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    //@Cacheable(cacheNames = "query",key = "#id",unless="#result == null")
    public ServiceVO queryServiceById(@PathVariable(value = "id") Long id) {
        ServiceVO serviceVo = serviceService.queryServiceById(id);
        return serviceVo;

    }

    /**
     * 新增服务
     *
     * @return
     */
    @PostMapping
    public boolean addService(@RequestBody AddServiceVO addServiceVo) {
        return serviceService.addService(addServiceVo);
    }

    /**
     * 分页条件查询服务项
     *
     * @param page
     * @param pageSize
     * @param code
     * @param name
     * @param subServiceTypeId
     * @param status
     * @param storeId
     * @return
     */
    @GetMapping
    public Pager<ServicePO> queryPage(@RequestParam(value = "page", required = false, defaultValue = "1") Long page,
                                      @RequestParam(value = "pageSize", required = false, defaultValue = "10") Long pageSize,
                                      @RequestParam(value = "code", required = false) String code,
                                      @RequestParam(value = "name", required = false) String name,
                                      @RequestParam(value = "subServiceTypeId", required = false) Long subServiceTypeId,
                                      @RequestParam(value = "status", required = false) Integer status,
                                      @RequestParam(value = "storeId", required = false) Long storeId) {
        return new Pager(serviceService.queryPage(page, pageSize, code, name, subServiceTypeId, status, storeId));


    }

    /**
     * 根据id删除服务
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    //@CacheEvict(cacheNames ="query",key = "#id")
    public boolean deleteService(@PathVariable(value = "id") Long id) {
        return serviceService.deleteService(id);
    }

    /**
     * 根据id更新服务项
     *
     * @param vo
     * @return
     */
    @PutMapping("/{id}")
    //@CachePut(cacheNames ="query",key = "#id")
    public boolean updateService(@PathVariable(value = "id") Long id, @RequestBody AddServiceVO vo) {
        ServicePO po = new ServicePO();
        BeanUtils.copyProperties(vo, po);
        po.setId(id);
        return serviceService.updateService(po, vo.getInfo(), vo.getDetails());
    }

    /**
     * 根据ids查询列表
     *
     * @param ids
     * @return
     */
    @PostMapping("/list")
    public List<ServiceDTO> getListById(@RequestBody List<Long> ids) {
        return serviceService.getListById(ids);
    }
}
